package com.aimashi.railwaytoolmanagesystem.activity.sendcard;

import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.aimashi.commonlib.component.STextFiledStr;
import com.aimashi.commonlib.util.MessageUtil;
import com.aimashi.commonlib.util.SoundUtil;
import com.aimashi.commonlib.util.StringUtil;
import com.aimashi.railwaytoolmanagesystem.R;
import com.android.hdhe.uhf.reader.Tools;
import com.android.hdhe.uhf.reader.UhfReader;
import com.lidroid.xutils.util.LogUtils;

/**
 * 银行卡操作的类
 * 
 * @author zhaoys
 * 
 */
public class SendCard extends Fragment{
	private View mMainView;
	
	private UhfReader reader ; //读取工具类 
	
	private STextFiledStr textFildReadEpc;
	
	private STextFiledStr textFildWriteEpc;
	
	private STextFiledStr textFildToolDescription;
	
	/**
	 * 读取标签
	 */
	private Button btnReadCard;
	
	/**
	 * 写入标签
	 */
	private Button btnWriteCard;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		reader = UhfReader.getInstance();
		if(reader == null){
			MessageUtil.alert(getActivity(), "获取读写器实例失败", Toast.LENGTH_LONG);
		}else{
			MessageUtil.alert(getActivity(), "获取读写器实例成功", Toast.LENGTH_SHORT);
			reader.setOutputPower(26);
		}
		LogUtils.i( "fragment1-->onCreate()");
		LayoutInflater inflater = getActivity().getLayoutInflater();
		
		mMainView = inflater.inflate(R.layout.sendcard, (ViewGroup)getActivity().findViewById(R.id.setting_viewPager), false);
		textFildReadEpc = (STextFiledStr)mMainView.findViewById(R.id.tool_read_epc);
		textFildWriteEpc = (STextFiledStr)mMainView.findViewById(R.id.tool_epc);
		textFildToolDescription = (STextFiledStr)mMainView.findViewById(R.id.tool_description);
		btnReadCard = (Button)mMainView.findViewById(R.id.sendcard_read_card);
		btnReadCard.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				List<byte[]> tempList =  reader.inventoryRealTime();
				Integer tempSize = tempList.size();
				if(tempSize == 0){
					SoundUtil.callAlarmAsShiBai(getActivity());
				}else{
					SoundUtil.callAlarm(getActivity());
					if(tempSize > 1){
						MessageUtil.alert(getActivity(), "读取到多个标签，请重新读取", Toast.LENGTH_SHORT);
						return;
					}
				}
				for(byte[] epc:tempList){
					String epcStr = Tools.Bytes2HexString(epc, epc.length);
					textFildReadEpc.setText(epcStr);
				}
			}
		});
		
		btnWriteCard = (Button)mMainView.findViewById(R.id.sendcard_write_card);
		btnWriteCard.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String oldEpcData = textFildReadEpc.getText();
				String tempWriteData = textFildWriteEpc.getText();
				String tempWriteDesc = textFildToolDescription.getText();
				
				if(!verificationEpc(tempWriteData)){
					MessageUtil.alert(getActivity(), "写入数据格式不正确", Toast.LENGTH_SHORT);
					return;
				}
				
				if(!StringUtil.isNull(tempWriteDesc)){
					MessageUtil.alert(getActivity(), "工具描述不能为空", Toast.LENGTH_SHORT);
					return;
				}
				
				byte[] accessPassword = Tools.HexString2Bytes("00000000");
				byte[] dataBytes = Tools.HexString2Bytes(tempWriteData);
				//dataLen = dataBytes/2 dataLen是以字为单位的
				reader.selectEPC(Tools.HexString2Bytes(oldEpcData));
				boolean writeFlag = reader.writeTo6C(accessPassword, 1, 2, 6, dataBytes);
				if(writeFlag){
					MessageUtil.alert(getActivity(), "写数据成功", Toast.LENGTH_SHORT);
				}else{
					MessageUtil.alert(getActivity(), "写数据失败", Toast.LENGTH_SHORT);
				}
			}
		});
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		LogUtils.i( "fragment1-->onCreateView()");
		
		ViewGroup p = (ViewGroup) mMainView.getParent(); 
        if (p != null) { 
            p.removeAllViewsInLayout(); 
            LogUtils.i( "fragment1-->移除已存在的View");
        } 
		
		return mMainView;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		LogUtils.i( "fragment1-->onDestroy()");
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		LogUtils.i( "fragment1-->onPause()");
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		LogUtils.i( "fragment1-->onResume()");
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		LogUtils.i( "fragment1-->onStart()");
	}

	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		LogUtils.i( "fragment1-->onStop()");
	}
	
	/**
	 * 验证EPC正确性
	 * @param pEpc
	 * @return
	 */
	private boolean verificationEpc(String pEpc){
		boolean falg = false;
		if(StringUtil.isNull(pEpc) 
			&& pEpc.length() == 24 ////强制用户输入24位字符
			){
			  String regex = "^[a-f0-9A-F]+$"; //falg
			  falg =  pEpc.matches(regex);
			}
		return falg;
	}
	
}
